package com.groupbuying.orderconsumer.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author gxy
 * @Description 时间转换工具类
 * @Date 2019/1/28 9:12
 **/
public class DateUtils {

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
    /**
     * dd-MM-yyyy
     */
    public static final String DMY = "dd-MM-yyyy";
    /**
     * MM-dd-yyyy
     */
    public static final String MDY = "MM-dd-yyyy";
    /**
     * yyyy-MM-dd
     */
    public static final String YMD = "yyyy-MM-dd";
    /**
     * dd-MM-yyyy HH:mm
     */
    public static final String DMYHM = "dd-MM-yyyy HH:mm";

    /**
     * @Author gxy
     * @Description 时间转换成文本
     * @Date 9:18 2019/1/28
     * @Param [date, formatPattern]
     * @return java.lang.String
     **/

    public static String format(Date date, String formatPattern) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat f = new SimpleDateFormat(formatPattern);
        return f.format(date);
    }


    /**
     * @Author gxy
     * @Description 文本转换成时间
     * @Date 9:18 2019/1/28
     * @Param [text, formatPattern]
     * @return Date
     **/
    public static Date parse(String text, String formatPattern) throws ParseException {
        if (StringUtils.isBlank(text)) {
            return null;
        }
        SimpleDateFormat f = new SimpleDateFormat(formatPattern);
        return f.parse(text);
    }



}
